package com.springBoot.commonClass;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.UUID;

public class Utils {

    public static String getUuid(){
        String uuid = UUID.randomUUID().toString().replace("-", "");
        return uuid;
    }

    public static void setFieldValueByName(String fieldName, Object o, String value){
        try{
//            String firstLetter = fieldName.substring(0, 1).toUpperCase();
//            String setter = "set" + firstLetter + fieldName.substring(1);
//            Method method = o.getClass().getMethod(setter, new Class[]{});
//            method.invoke(o, new Object[]{});
            Field field = o.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            field.set(o, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getFieldValueByName(String fieldName, Object o){
        String value = "";
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getDeclaredMethod(getter, new Class[]{});
            value = (String)method.invoke(o, new Object[]{});
        }catch (Exception e){
            e.printStackTrace();
        }
        return value;
    }
}
